<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/11 0011
 * Time: 16:40
 */

namespace app\api\controller\v1\cleaner;


use app\api\controller\v1\Cleaner;
use app\currency\service\CleanerService;
use think\Db;

class Team extends Cleaner
{
    /**
     * 团队列表 全部、一级、二级
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $state = (int)$this->request->get('state',0);
        in_array($state,[0,1,2]) || $state = 0;
        $db = $this->_query('Cleaner');
        if($state == '1'){
            $db->where('pid','eq',$this->cid);
        } else {
            $childMemberIds = Db::name('Cleaner')->where('pid','eq',$this->cid)->column('id');
            if($state == '2' && !empty($childMemberIds)){
                $db->where('pid','in',$childMemberIds);
            } elseif ($state == '0'){
                $db->where('pid','in',array_merge([$this->cid],$childMemberIds));
            } else {
                $db->where('0=1');
            }
        }
        $list = $db->page(true,false,false,10);

        foreach ($list['list'] as &$item) {
            //团队人数
            $item['team_num'] = CleanerService::getTeamNum($item['id'],1,true);
            //绑定地址数量
            $item['bind_add_num'] = CleanerService::getBindAddNum($item['id']);
            //清洁提成
            $item['royalty'] = Db::name('CleanOrderRoyalty')->where([['cid','eq',$this->cid],['ccid','eq',$item['id']]])->sum('amount')+0;
            if($item['pid'] == $this->cid)
            {
                $item['level'] = 1;
            } else {
                $item['level'] = 2;
            }
        }
        //一级团队数量
        $list['one_level_child_num'] = CleanerService::getTeamNum($this->cid,1);
        //二级团队数量
        $list['two_level_child_num'] = CleanerService::getTeamNum($this->cid,2);
        $this->success('获取团队列表成功！',$list);

    }

    /**
     * 成员详情
     * @param $id
     */
    public function read($id)
    {
        $cleaner = Db::name('Cleaner')
            ->where([['id','eq',$id],['status','eq','1']])
            ->find();
        empty($cleaner) && $this->error('查询用户信息失败！');
        $sub_user = [];
        if($cleaner['pid'] == $this->cid)
        {
            //查询下级
            $sub_user = (array)Db::name('Cleaner')
                ->where([['pid','eq',$id],['status','eq','1']])
                ->field('id,name,work_headimg')
                ->order('create_at desc')
                ->select();
        }
        //查询清洁提成
        $royalty_log = Db::name('CleanOrderRoyalty')
            ->alias('a')
            ->join('clean_order b','a.order_no=b.order_no')
            ->where([
                ['a.cid','eq',$this->cid],
                ['a.ccid','eq',$id],
                ['a.modal','eq','service']
            ])
            ->field('a.*,b.cities_id')
            ->limit(10)
            ->order('create_at desc')
            ->select();
        $result = [
            'userinfo' => $cleaner,
            'sub_user' => $sub_user,
            'royalty_log' => $royalty_log
        ];
        $this->success('获取成员详情成功！',$result);
    }
}